package tetris.control;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JOptionPane;
/**
 * 
 * @author cyr
 *
 */
public class PlayerFile {

	private static String playerName;
	private String playerPassword;
	private static int hightestScore;
	private static String rtPassword;
	private static String str = "test/player.txt";
	
	public PlayerFile(String playerName,String playerPassword) {
		this.playerPassword=playerPassword;
		PlayerFile.playerName=playerName;
	}
	public static boolean writeFile(String playerName, String playerPassword) throws IOException {

		if ("".equals(playerName) || "".equals(playerPassword)) {
			JOptionPane.showMessageDialog(null, "Player Name or Password Error!");
			return false;
		}
		File f = new File(str);
		if (queryFile(playerName)) {
			JOptionPane.showMessageDialog(null, "Player Name Has Exited!");
			return false;
		}
		String str1 = "";
		str1 += playerName + " " + playerPassword + " " + hightestScore;
		try {
			FileWriter fw = new FileWriter(f, true);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(str1);
			bw.newLine();
			bw.flush();
			bw.close();
			fw.close();

		} catch (Exception e) {
			System.out.println(e);
			return false;
		}
		JOptionPane.showMessageDialog(null, "Register Succeed!");
		return true;
	}

	public static boolean queryFile(String playerName) throws IOException {

		File f = new File(str);
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String str2 = br.readLine();
		while (str2 != null) {
			String[] arr = new String[3];
			arr = str2.split(" ");
			if (arr[0].equals(playerName)) {
				rtPassword = arr[1];
				fr.close();
				br.close();
				return true;
			}
			str2 = br.readLine();
		}
		fr.close();
		return false;
	}

	public static boolean matchPassword(String playerName1, String playerPassword) throws IOException {
		if (!queryFile(playerName1)) {
			return false;
		}
		if (playerPassword.equals(rtPassword)) {
			playerName = playerName1;
			return true;
		}
		JOptionPane.showMessageDialog(null, "Player Name or Password Error!");
		return false;
	}

	public static void renewFile(int Score) throws IOException {
		File f1 = new File(str);
		File f2 = new File(str);
		FileReader fr = new FileReader(f1);
		BufferedReader br = new BufferedReader(fr);
	
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f2,true)));
        String str2;
        while((str2=br.readLine())!=null){
        	String[] arr = new String[3];
			arr = str2.split(" ");
            if(arr[0].equals(playerName)){
            	//String str3 = arr[0] + " " + arr[1] + " " + Score;
            	String score=""+Score;
                pw.println(str2.replace(arr[2],score));  
            }else{
                pw.println(str2);
            }
        }
        br.close();
        pw.close();
    }
	@SuppressWarnings("resource")
	public static List<Entry<String, String>> outputCharts() throws IOException {
		File f = new File(str);
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String str2 = br.readLine();
		String name = "";
		String score = "";
		Map<String, String> hashmap = new HashMap<>();
		while (str2 != null) {
			String[] arr = new String[3];
			arr = str2.split(" ");
			name = arr[0];
			score = arr[2];
			hashmap.put(name, score);
			str2 = br.readLine();
		}
		List<Map.Entry<String, String>> lis = new ArrayList<Map.Entry<String, String>>(hashmap.entrySet());
		Collections.sort(lis, new Comparator<Map.Entry<String, String>>() {
			@Override
			public int compare(Map.Entry<String, String> e1, Map.Entry<String, String> e2) {
				return e2.getValue().compareTo(e1.getValue());
			}
		});
		for(Entry<String, String> list:lis)
		{
			System.out.println(list);
		}
		return lis;
	}
}